import numpy as np
import trimesh
import os

# 在这里直接写死要处理的OBJ路径列表
INPUT_PATHS = [
    "output_1.obj",
    "output_2.obj",
    "output_3.obj",
    "output_4.obj",
    "output_5.obj",
    "output_6.obj",
    "output_7.obj",
    "output_8.obj",
    "output_9.obj",
    "output_10.obj",
    "output_11.obj",
    "output_12.obj",
    "output_13.obj",
    "output_14.obj",
    "output_15.obj",
    "output_16.obj",
    "output_17.obj",
    "output_18.obj",
]

def process_single_file(input_path):
    """处理单个OBJ文件"""
    if not os.path.isfile(input_path):
        print(f"文件不存在，跳过: {input_path}")
        return
    if not input_path.lower().endswith('.obj'):
        print(f"非OBJ文件，跳过: {input_path}")
        return

    try:
        mesh = trimesh.load(input_path)
        points, _ = trimesh.sample.sample_surface(mesh, 20000)
        
        rotation = trimesh.transformations.random_rotation_matrix()
        translation = trimesh.transformations.translation_matrix(np.random.uniform(-1, 1, 3))
        transform = trimesh.transformations.concatenate_matrices(rotation, translation)
        
        cloud = trimesh.PointCloud(points)
        cloud.apply_transform(transform)
        
        base_path = os.path.splitext(input_path)[0]
        output_ply = base_path + '.ply'
        
        # 显式指定ASCII编码（解决encoding报错）
        cloud.export(output_ply, encoding='ascii')  # 关键修改
        print(f"成功生成: {output_ply}")
    
    except Exception as e:
        print(f"处理 {input_path} 时出错: {str(e)}")

def main():
    print(f"开始处理 {len(INPUT_PATHS)} 个文件...")
    for path in INPUT_PATHS:
        process_single_file(path)
    print("全部处理完成")

if __name__ == '__main__':
    main()
